﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ISRecord.Model;
using System.Collections.ObjectModel;
using ISRecord.View.Services;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Data;
using GalaSoft.MvvmLight;

namespace ISRecord.View.ViewsModel
{
    public sealed class SearchOutboxViewModel:ViewModelBase
    {
        #region "Fields"
        string outboxNo;
        //string outboxDate;
        string subject;
        string goingTo;
        DelegateCommand filterCommand;
        //DelegateCommand filterBySubjectCommand;
        //DelegateCommand filterByGoingToCommand;
        #endregion

        #region "Constructor"
        public SearchOutboxViewModel(ObservableCollection<Outbox> outboxes)
        {
            Outboxes = CollectionViewSource.GetDefaultView(outboxes);
            Outboxes.CurrentChanged +=new EventHandler(SelectedItemChanged);
            
        }
        #endregion


        #region "Hepler Methods"
        private void SelectedItemChanged(object sender, EventArgs e)
        {
            SelectedOutbox = Outboxes.CurrentItem as Outbox;
        }

        private bool FilterByOutboxNo(object outbox)
        { 
            Outbox o = outbox as Outbox;
            return o.OutboxNo == this.OutboxNo;
        }
        private bool FilterByGoingTo(object outbox)
        {
            Outbox o = outbox as Outbox;
            return o.GoingTo.Contains(this.GoingTo);
        }
        private bool FilterBySubject(object outbox)
        {
            Outbox o = outbox as Outbox;
            return o.Subject.Contains(this.Subject);
        }
        
        #endregion

        #region "BindableProperties"
        public string OutboxNo
        {
            get { return outboxNo; }
            set 
            {
                if (outboxNo != value)
                {
                    outboxNo = value;
                    RaisePropertyChanged("OutboxNo");
                    
                }
            }
        }
        public string Subject
        {
            get { return subject; }
            set
            {
                if (subject != value)
                {
                    subject = value;
                    RaisePropertyChanged("Subject");
                }
            }
        }
        public string GoingTo
        {
            get { return goingTo; }
            set
            {
                if (goingTo != value)
                {
                    goingTo = value;
                    RaisePropertyChanged("GoingTo");
                }
            }
           
        }

        public ICollectionView Outboxes
        {
            get;
            private set;
        }
        #endregion

        #region "Other Properties"
        public Outbox SelectedOutbox
        {
            get;
            private set;
        }
        
        #endregion
        #region "Commands"
        public ICommand FilterCommand
        {
            get
            {
                if (filterCommand == null)
                {
                    filterCommand = new DelegateCommand(Filter, CanFilter, "Find");
                }
                return filterCommand;
            }
        }
        const string FILTERBYOUTBOXNO ="ByOutbox";
        const string FILTERBYSUBJECT = "BySubject";
        const string FILTERBYGOINGTO = "ByGoingTo";
        private void Filter(object obj)
        {
            if(obj == null) throw new ArgumentNullException("Filter option must be set");

            if (obj.ToString().Equals(FILTERBYOUTBOXNO) && !string.IsNullOrEmpty(this.OutboxNo))
            {
                Outboxes.Filter += FilterByOutboxNo;
               
                
            }
            else if(obj.ToString().Equals(FILTERBYSUBJECT) && !string.IsNullOrEmpty(this.Subject) )
            {
                Outboxes.Filter += FilterBySubject;
            }
            else if (obj.ToString().Equals(FILTERBYGOINGTO) && !string.IsNullOrEmpty(this.GoingTo))
            {
                Outboxes.Filter += FilterByGoingTo;
            }
            else
            {
                if (Outboxes.Filter != null)
                {
                    Outboxes.Filter = null;
                }
            }
            Outboxes.Refresh();
            
        }
        private bool CanFilter(object obj)
        {
            return (Outboxes != null || !Outboxes.IsEmpty);
        }
        #endregion

        #region "General Methods"
        public static IList<Outbox> GetAll()
        {
            using (OutboxRepository rep = new OutboxRepository( new Sender2006Entities()))
            {
                return rep.GetAll();
            }
        }
        #endregion

        #region "Base Implementation"
        public override void Cleanup()
        {
            Outboxes.CurrentChanged -= SelectedItemChanged;
            base.Cleanup();
        }

        #endregion

        public bool ModelHasChanges
        {
            get { return false; }
        }
        public string DisplayName { get; private set; }
    }
}
